package com.huluobo.a2107mvpimdemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义字母条
 * 1.自定义view需要继承一个view,并且重写构造方法,有4个构造,
 * 1个参数的构造,是在java中使用的,
 * 2个参数的一般用于xml中,第二个参数代表一个view中的属性
 * Created by LC on 2023/9/27.
 */
public class SlideBar extends View {

    //字母条的数据源
    private List<String> SECTIONS = new ArrayList<>();

    //画笔
    private Paint paint;

    //一个字母的高度
    private float sectionHeight = 0f;

    private float textBaseline = 0f;//定义基准线


    public SlideBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        //初始化字母
        SECTIONS.add("A");
        SECTIONS.add("B");
        SECTIONS.add("C");
        SECTIONS.add("D");
        SECTIONS.add("E");
        SECTIONS.add("F");
        SECTIONS.add("G");
        SECTIONS.add("H");
        SECTIONS.add("I");
        SECTIONS.add("J");
        SECTIONS.add("K");
        SECTIONS.add("L");
        SECTIONS.add("M");
        SECTIONS.add("N");
        SECTIONS.add("O");
        SECTIONS.add("P");
        SECTIONS.add("Q");
        SECTIONS.add("R");
        SECTIONS.add("S");
        SECTIONS.add("T");
        SECTIONS.add("U");
        SECTIONS.add("V");
        SECTIONS.add("W");
        SECTIONS.add("X");
        SECTIONS.add("Y");
        SECTIONS.add("Z");

        //初始化画笔
        paint = new Paint();
        paint.setColor(Color.GRAY);
        paint.setTextSize(30);
        paint.setTextAlign(Paint.Align.CENTER);//设置绘制文字的位置,从中心开始绘制
    }

    //当View的高度发生变化时的回调
    //h参数就是当前view的高度,所以让他除以26,就是一个字母的高度
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        //计算出每一个字母的高度
        sectionHeight = h * 1.0f / SECTIONS.size();
        //以下是基准线定义公式
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();//d获取绘制文字的尺寸
        float textHeight = fontMetrics.descent - fontMetrics.ascent;//获取文字的高度
        textBaseline = sectionHeight / 2 + (textHeight / 2 - fontMetrics.descent);//基准线计算公式
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float x = getWidth() * 1.0f / 2;//绘制的x轴
//        float y = sectionHeight;//绘制的y轴
        float baseline = textBaseline;

        for (int i = 0; i < SECTIONS.size(); i++) {
//            canvas.drawText(SECTIONS.get(i), x, y, paint);
//            y += sectionHeight;
            canvas.drawText(SECTIONS.get(i), x, baseline, paint);
            baseline += sectionHeight;
        }
    }

    //触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://手指按下
                setBackgroundColor(Color.DKGRAY);
                onSectionChangeListener.onSectionChange(SECTIONS.get(getTouchIndex(event)));
                break;
            case MotionEvent.ACTION_MOVE://移动
                onSectionChangeListener.onSectionChange(SECTIONS.get(getTouchIndex(event)));
                break;
            case MotionEvent.ACTION_UP://手指抬起
                setBackgroundColor(Color.TRANSPARENT);
                onSectionChangeListener.onSlideFinish();
                break;
        }
        return true;//消费事件,不再继续向下传递
    }

    private int getTouchIndex(MotionEvent event) {
        int index = (int) (event.getY() / sectionHeight);//event.getY,按下时会产生一个Y值,通过去除以每个item的高度,来拿到一个index下标,整数
        if (index < 0) {
            index = 0;
        } else if (index >= SECTIONS.size()) {
            index = SECTIONS.size() - 1;
        }
        return index;
    }

    //接口回调3步
    //第一步,定义一个接口
    public interface OnSectionChangeListener {
        void onSectionChange(String firstLetter);//回调出首字符

        void onSlideFinish();//手指抬起的回调
    }

    //第二步,声明这个接口
    private OnSectionChangeListener onSectionChangeListener;

    //第三步,声明这个接口的set方法
    public void setOnSectionChangeListener(OnSectionChangeListener onSectionChangeListener) {
        this.onSectionChangeListener = onSectionChangeListener;
    }
}
